<?php
namespace app\sports\match\model;

use think\Db;
use think\Model;

class MatchModel extends Model
{
    private $database = 'sports_database';

    //进球
    const TYPE_ONE = 1;
    //角球
    const TYPE_TWO = 2;
    //换人
    const TYPE_NINE = 9;
    //结束
    const TYPE_TWELVE = 12;

    //主队
    const POSITION_HOME = 1;
    //客队
    const POSITION_AWAY = 2;


    /**
     * 根据ID获取比赛信息
     */
    public function getMatchInfoByMatchId($matchId)
    {
        return Db::database($this->database)->table('foot_ball_team_match')->where('nami_match_id', $matchId)->find();
    }

    /**
     * 根据比赛ID获取阵容
     */
    public function getLineUpByMatchId($matchId)
    {
        return Db::database($this->database)->table('foot_ball_lineup')->where('nami_match_id', $matchId)->find();
    }

    /**
     * 根据球队获取球队信息
     */
    public function getTeamInfoByTeamId($teamId)
    {
        return Db::database($this->database)->table('foot_ball_team')->where('nami_team_id', $teamId)->find();
    }

    /**
     * 获取阵容详情
     */
    public function getLineUpDetail($matchId)
    {
        return Db::database($this->database)->table('foot_ball_team_lineup_detail')->where('nami_match_id', $matchId)->select();
    }

    /**
     * 根据赛事ID获取赛事信息
     */
    public function getEventInfoById($eventId)
    {
        return Db::database($this->database)->table('foot_ball_events')->where('event_id', $eventId)->find();
    }

    /**
     * 根据比赛ID获取情报和预测
     */
    public function getIntelligenceByMatchId($matchId)
    {
        return Db::database($this->database)->table('foot_ball_intelligence')->where('nami_match_id', $matchId)->find();
    }

    /**
     * 根据比赛ID获取数据
     */
    public function getAnalysisByMatchId($matchId)
    {
        return Db::database($this->database)->table('foot_ball_intelligence')->where('nami_match_id', $matchId)->find();
    }

    /**
     * 根据队员ID获取数据（批量）
     */
    public function getPlayerInfoByIdArr($playerId)
    {
        $data = Db::database($this->database)->table('foot_ball_team_player')->whereIn('id', $playerId)->select();
        $result = [];
        foreach ($data as $value) {
            $result[$value['nami_player_id']] = $value;
        }

        return $result;
    }

    /**
     * 根据比赛ID和时间类型获取本场数据
     */
    public function getIncidentsByMatchId($matchId, $type = 0)
    {
        $list = Db::database($this->database)->table('foot_ball_tlive')->where('nami_match_id', $matchId);
        if ($type) {
            $list = $list->where('type', $type);
        }

        $list = $list->select();

        return $list;
    }

}